package com.example.demo.nuoqi.utils;/*
 *author limfff
 *date 2021/5/19
 */

import com.example.demo.nuoqi.pojo.Itemdetails;
import com.example.demo.nuoqi.pojo.User;
import com.example.demo.nuoqi.service.ItemdetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

public class ItemUtils {


    @Autowired
    ItemdetailsService itemdetailsService;

    //Gt 大于 如果返回true 代表 项目人数已满, false 人数没满 还可加人
    public static boolean ItemPersonNumLtGt(Itemdetails itemdetails) {
        if (null != itemdetails && null != itemdetails.getParticipant()) {
            List<Itemdetails.participantTemp> toList = JsonUtils.jsonToList(itemdetails.getParticipant(), Itemdetails.participantTemp.class);
            if (toList.size() <= itemdetails.getItemPersonNum()) {
                return false;
            }else{
                return true;
            }
        } else {
            return true;
        }
    }


    public static Result addItemPartPerson(Itemdetails itemdetails , User user) {

        String participant = itemdetails.getParticipant();

        List<Itemdetails.participantTemp> toList = JsonUtils.jsonToList(participant, Itemdetails.participantTemp.class);
        List<Itemdetails.participantTemp> temp = new ArrayList<>();

        Itemdetails.participantTemp participantTemp = new Itemdetails.participantTemp();
        participantTemp.setUserId(user.getId());
        participantTemp.setUserName(user.getName());
            // 有参与者
            if (toList.size() >= 1) {
                toList.add(participantTemp);
                String result = JsonUtils.objectToJson(toList);
                itemdetails.setParticipant(result);
            }
            // 无参与者
            else{
                temp.add(participantTemp);
                String result = JsonUtils.objectToJson(temp);
                itemdetails.setParticipant(result);
            }
            return new Result(true,StatusCode.OK,"加入成功",itemdetails);
    }


    public static Result delItemPartPerson(Itemdetails itemdetails , User user) {

        String participant = itemdetails.getParticipant();

        List<Itemdetails.participantTemp> toList = JsonUtils.jsonToList(participant, Itemdetails.participantTemp.class);
        List<Itemdetails.participantTemp> temp = new ArrayList<>();
        temp.addAll(toList);
        Itemdetails.participantTemp participantTemp = new Itemdetails.participantTemp();
        participantTemp.setUserId(user.getId());
        participantTemp.setUserName(user.getName());


            // 有参与者
        for (Itemdetails.participantTemp participantTemp1 : toList) {
            if (participantTemp1.getUserId() == user.getId()) {
                temp.remove(participantTemp1);
            }
        }
        itemdetails.setParticipant(JsonUtils.objectToJson(temp));
        return new Result(true,StatusCode.OK,"项目移除人成功",itemdetails);
    }
}
